﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Tilver.Coder.CommandExcuteResult;

namespace Tilver.Coder.Core
{
    /// <summary>
    /// 存储过程
    /// </summary>
    [Serializable]
    public class CoderCommand : CoderTabular
    {
        /// <summary>
        /// 类型
        /// </summary>
        public override TabularTypeEnum TabularType
        {
            get
            {
                return TabularTypeEnum.Command;
            }
        }
        /// <summary>
        /// 执行结果
        /// </summary>
        public ExecuteResultBase CommandResult { get; set; }
        /// <summary>
        /// 列集合
        /// </summary>
        public override List<CoderColumn> Columns
        {
            get
            {
                if (CommandResult != null && 
                    (CommandResult.CommandResult == ExecuteResultEnum.ExecuteSingle || CommandResult.CommandResult == ExecuteResultEnum.ExecuteReader))
                {
                    return ((ExecuteSingleResult)CommandResult).Columns;
                }
                else
                {
                    return null;
                }
            }
            
        }
        /// <summary>
        /// 命令文本
        /// </summary>
        public string CommandText { get; set; }
        private List<CoderParameter> _parameters;
        /// <summary>
        /// 参数集合
        /// </summary>
        public List<CoderParameter> Parameters 
        {
            get { return _parameters ?? (_parameters = new List<CoderParameter>()); }
            set { _parameters = value; }
        }

        /// <summary>
        /// 输入参数集合
        /// </summary>
        public IEnumerable<CoderParameter> InputParameters
        {
            get
            {
                return Parameters.Where(w => w.Direction == ParameterDirection.Input || w.Direction == ParameterDirection.InputOutput);
            }
        }
        /// <summary>
        /// 输出参数集合
        /// </summary>
        public IEnumerable<CoderParameter> OutputParameters
        {
            get
            {
                return Parameters.Where(w => w.Direction == ParameterDirection.Output || w.Direction == ParameterDirection.InputOutput);
            }
        }

    }
}
